package com.meeting.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.meeting.entity.MeetingRoomStatus;
import com.meeting.mapper.MeetingRoomStatusMapper;
import com.meeting.model.enums.MeetingRoomStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class BitUtil {

    @Resource
    private MeetingRoomStatusMapper meetingRoomStatusMapper;

    /**
     * 设置会议室时间
     * @param meetingRoomId
     * @param startTime
     * @param endTime
     */
    public void setTimeBit(Long meetingRoomId, Date startTime, Date endTime, Integer i) throws ParseException {
        String meetingRoomTime = getMeetingRoomTime(meetingRoomId, startTime);
        StringBuilder sb = new StringBuilder(meetingRoomTime);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endTime);
        for (int hour = startCalendar.get(Calendar.HOUR_OF_DAY); hour < endCalendar.get(Calendar.HOUR_OF_DAY); hour++) {
            sb.setCharAt(hour, String.valueOf(i).charAt(0));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(startTime);
        startTime = sdf.parse(format);

        // 先查询有没有该日期的数据，如果没有就新增一条数据
        LambdaQueryWrapper<MeetingRoomStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MeetingRoomStatus::getMeetingRoomId, meetingRoomId)
                .eq(MeetingRoomStatus::getDate, startTime);
        if (meetingRoomStatusMapper.selectOne(queryWrapper) == null) {
            MeetingRoomStatus meetingRoomStatus = new MeetingRoomStatus();
            meetingRoomStatus.setMeetingRoomId(meetingRoomId);
            meetingRoomStatus.setDate(startTime);
            meetingRoomStatus.setStatus(sb.toString());
            meetingRoomStatusMapper.insert(meetingRoomStatus);
        }
        LambdaUpdateWrapper<MeetingRoomStatus> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MeetingRoomStatus::getMeetingRoomId, meetingRoomId)
                .eq(MeetingRoomStatus::getDate, startTime)
                .set(MeetingRoomStatus::getStatus, sb.toString());

        meetingRoomStatusMapper.update(wrapper);
    }

    /**
     * 判断会议室是否冲突
     * @param meetingRoomId
     * @param startTime
     * @param endTime
     * @return
     */
    public boolean isConflict(Long meetingRoomId, Date startTime, Date endTime) {
        String meetingRoomTime = getMeetingRoomTime(meetingRoomId, startTime);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endTime);
        for (int i = startCalendar.get(Calendar.HOUR_OF_DAY); i < endCalendar.get(Calendar.HOUR_OF_DAY); i++) {
            int i1 = meetingRoomTime.charAt(i) - '0';
            if (i1 != MeetingRoomStatusEnum.FREE.getCode()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取会议室时间
     * @param meetingRoomId
     * @param date
     * @return
     */
    public String getMeetingRoomTime(Long meetingRoomId, Date date) {
        // 格式化date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        LambdaQueryWrapper<MeetingRoomStatus> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingRoomStatus::getMeetingRoomId, meetingRoomId)
                .eq(MeetingRoomStatus::getDate, date);
        MeetingRoomStatus meetingRoomStatus = meetingRoomStatusMapper.selectOne(wrapper);
        return meetingRoomStatus != null ? meetingRoomStatus.getStatus() : "4".repeat(8) + "0".repeat(13) + "4".repeat(3);
    }
}
